// pages/index/store_admin/goods_edit.js
Page({

  data: {   shebeilist:{},store:[],arr:[],index:0,arr1:['打印机','复印机'],index1:0,arr2:['企业','个人','商铺'],index2:0,url:'',
  speclist:[],arr3:[],specxq:[],arrzong:[],arrzong_id:[],arr7:[],title:'',deviceld:'',devicekey:'',devicepost:'',pintermodel:'',bonus:'',number:0,
  url1:'',type:'',arr9:[], shebeitype:[],index3:0

  },

  onLoad: function (options) {
    
   this.setData({
     id:options.id,
     type:options.type
   })
   this.gettianjia()
   if(options.type==2){this.getshareshop1()}
   else{
   this.getshareshop()}
   
  },
  onShow(){
      this.gettianjia()
  },
   //获取修改设备的数据
  
   bindPickerChange(e){
     
     
     this.setData({
       index:e.detail.value
     })
   },
   bindPickerChange1(e){
     
     
    this.setData({
      index1:e.detail.value
    })
  },
  bindPickerChange2(e){
     
     
    this.setData({
      index2:e.detail.value
    })
  },
  remove(){
     this.setData({url:''})
  },
  getimglist(){
    console.log(111);
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        wx.uploadFile({
          filePath: res.tempFiles[0].path,
          name: 'file',
          url: 'https://zujieba.com/index/upload/upload.html',
          success:(res1)=>{
            console.log(res1);
            let data=JSON.parse(res1.data)
            let url="https://zujieba.com/file_index/"+data.name
            this.setData({url,url1:data.name})
          }
        })
      },
    })
    
  },
  toggle3(e){
    this.setData({arr7:[]})
      console.log(e.currentTarget.dataset.index,e.currentTarget.dataset.index1);
      let index=e.currentTarget.dataset.index
      let index1=e.currentTarget.dataset.index1
      let arr3=this.data.arr3
      console.log(this.data.arr3);
      
      arr3[index1].list[index].checked=!arr3[index1].list[index].checked
      this.setData({arr3})


      let arr4=[]
      let arr5=[]
      let arr6=[]
      let arr8=[]
      let arr4_id=[]
      let arr5_id=[]
      let arr6_id=[]
      let arr8_id=[]
      arr3.forEach((item,index)=>{
        item.list.forEach((item1)=>{
            if(item1.checked==true&&index==0){
              arr4.push(item1.title)
              arr4_id.push(item1.id)
              console.log(arr4);
              
            }else if(item1.checked==true&&index==1){
               arr5.push(item1.title)
               arr5_id.push(item1.id)
               console.log(arr5);
               
            }else if(item1.checked==true&&index==2){
              arr6.push(item1.title)
              arr6_id.push(item1.id)
              console.log(arr6);
              
            }else if(item1.checked==true&&index==3){
              arr8.push(item1.title)
              arr8_id.push(item1.id)
              console.log(arr6);
              
            }
        })
      })
      console.log(arr4);
   //这是拼接title的   
      let arrzong=[]
      let arrzong_id=[]
      let str
      let str_id
      if(arr4.length!=0){
      arr4.forEach((item)=>{
        console.log(11);
        
          arr5.forEach((item1)=>{
           /* if(arr6.length==0){
            console.log(22);
            str=item+'_'+item1
            
            arrzong.push(str)}
            else{
            */
            arr6.forEach((item2)=>{
                   arr8.forEach((item3)=>{
                    str=item+'_'+item1+'_'+item2+'_'+item3
                    arrzong.push(str)
                   })
              console.log(33);
              
               
              
              console.log(arrzong);
              
            })
          })
      })}/*else{
        arr5.forEach((item1)=>{
         
          
          arr6.forEach((item2)=>{

            console.log(33);
            
             str=item1+'_'+item2
            arrzong.push(str)
         
            
          })
        })

      }
      console.log(arrzong);*/
      this.setData({arrzong})
      //这是拼接id的
      if(arr4_id.length!=0){
        arr4_id.forEach((item)=>{
          console.log(11);
          
            arr5_id.forEach((item1)=>{
            /*  if(arr6_id.length==0){
              console.log(22);
              str_id=item+'_'+item1
              
              arrzong_id.push(str_id)}
              else{*/
              
              arr6_id.forEach((item2)=>{
  
                console.log(33);
                
                arr8_id.forEach((item3)=>{
                  str_id=item+'_'+item1+'_'+item2+'_'+item3
                  arrzong_id.push(str_id)
                })
                console.log(arrzong_id);
                
              })
            })
        })}/*else{
          arr5_id.forEach((item1)=>{
           
            
            arr6_id.forEach((item2)=>{
  
              console.log(33);
              
               str_id=item1+'_'+item2
              arrzong_id.push(str_id)
              console.log(arrzong_id);
              
            })
          })
  
        }*/
        console.log(arrzong_id);
        
      this.setData({
        arrzong_id
      })
      
      
     
      
  },
  gettianjia(){

    wx.request({
      url: 'https://zujieba.com/Api/storeadmingoods/add_goods.html',
      data:{status:2,uid:wx.getStorageSync('uid'),id:9},
      success:(res)=>{
        console.log(res);
        let store=res.data.data.store
        let speclist=res.data.data.spec
        let arr3=[]
        speclist.forEach((item)=>{
            if(item.level==1) {
               arr3.push(item)
               item.list=[]
            }
        })
        arr3.forEach((item1)=>{
           speclist.forEach((item2)=>{
              if(item1.id==item2.pid){
                 item1.list.push(item2)
                 item2.checked=false
              }
           })
        })

        let arr=[]
        store.forEach((item)=>{
            arr.push(item.title)
            this.setData({arr})
        })
        console.log(arr);
       
      this.setData({arr3})
       
        
      }})
  },
  change6(e){
    console.log(e);
    console.log(this.data.arr7);
    
    let arr7=this.data.arr7
    arr7[e.currentTarget.dataset.i]=e.detail.value
    console.log(arr7);
    
  },
  

   change1(e){
      this.setData({
        title:e.detail.value
      })
   },
   change2(e){
    this.setData({
      deviceld:e.detail.value
    })
 },
 change3(e){
  this.setData({
    devicekey:e.detail.value
  })
},
change4(e){
  this.setData({
    devicepost:e.detail.value
  })
},
change5(e){
  this.setData({
    pintermodel:e.detail.value
  })
  
  
},
change7(e){
  this.setData({
    bonus:e.detail.value
  })
},
change8(e){
  this.setData({
    number:e.detail.value
  })
},
   //提交
   suretianjia1(){
     console.log(this.data.pintermodel);
     
   let uid=wx.getStorageSync('uid')
   let pid=this.data.arr9[parseInt(this.data.index)].id
    let title=this.data.title
     let deviceld=this.data.deviceld
     let devicepost=this.data.devicepost
      let devicekey=this.data.devicekey
       let pintermodel=this.data.pintermodel
        let number=0
         let bonus=0
          let mune=2
           let statu=parseInt(this.data.index1)+1
           let pic=this.data.url1
            let price=this.data.arr7
              let spec_title=this.data.arrzong
                let  spec_id=this.data.arrzong_id

    let params={uid,pid,title,deviceld,devicekey,devicepost,pintermodel,number,bonus,mune,statu,pic,price,spec_id,spec_title,status:1,type:1,color:1,} 
        console.log(params);
        
    if(title!=''&&devicekey!=''&&devicepost!=''&&devicekey!=''&&pic!=''&&pintermodel!=''&&this.data.arr7.length!=0)  {
    wx.request({
      url: 'https://zujieba.com/Api/storeadmingoods/add_goods.html',
      data:params,
     method:'POST',
      success:(res)=>{
        console.log(res);
        if(res.data.success==1){
         wx.navigateBack({
           complete: (res) => {},
         })
        }else{
          wx.showToast({
            title: res.data.tips,
            icon:'none'
          })
        }
        
      }
    })     }else{wx.showToast({
      title: '请完善信息',
      icon:'none'
    })}    
 
},
// 共享商
 //提交
 suretianjia11(){
    
     console.log(this.data.pintermodel);
     

     
  let uid=wx.getStorageSync('uid')
   let pid=this.data.arr9[parseInt(this.data.index)].id
   let title=this.data.title
    let deviceld=this.data.deviceld
    let devicepost=this.data.devicepost
     let devicekey=this.data.devicekey
      let pintermodel=this.data.pintermodel
       let number=this.data.number
        let bonus=0
         let mune=2
          let statu=parseInt(this.data.index1)+1
          let pic=this.data.url1
           let price=this.data.arr7
             let spec_title=this.data.arrzong
               let  spec_id=this.data.arrzong_id

   let params={uid,pid,title,deviceld,devicekey,devicepost,pintermodel,number,bonus,mune,statu,pic,price,spec_id,spec_title,status:1,type:1,color:1,} 
       console.log(params);
       
   if(title!=''&&devicekey!=''&&devicepost!=''&&devicekey!=''&&pic!=''&&pintermodel!=''&&this.data.arr7.length!=0)  {
   wx.request({
     url: 'https://zujieba.com/Api/storeadmingoods/add_sharer_goods.html',
     data:params,
    method:'POST',
     success:(res)=>{
       console.log(res);
       if(res.data.success==1){
        wx.navigateBack({
          complete: (res) => {},
        })
       }else{
         wx.showToast({
           title: res.data.tips,
           icon:'none'
         })
       }
       
     }
   })     }else{wx.showToast({
     title: '请填写完整',
   })}    

},
//获取共享商的所属店铺
    getshareshop(){
       wx.request({
         url: 'https://zujieba.com/Api/storeadmingoods/sharer_store.html',
         data:{pid:wx.getStorageSync('uid')},
         method:'POST',
         success:(res)=>{
           console.log(res);
           let arr8=[]
           res.data.data.store.forEach((item)=>{
                 arr8.push(item.title)
                 
           })
           this.setData({arr:arr8,arr9:res.data.data.store})
           
         }
       })
    },
   //获取店家的店铺列表
   getshareshop1(){
    wx.request({
      url: 'https://zujieba.com/Api/storeadmingoods/add_goods.html',
      data:{uid:wx.getStorageSync('uid'),status:'2'},
      method:'POST',
      success:(res)=>{
        console.log(res);
        let arr8=[]
        res.data.data.store.forEach((item)=>{
              arr8.push(item.title)
              
        })
        this.setData({arr:arr8,arr9:res.data.data.store})
        
      }
    })
 },
  //获取设备型号
  getshebeitype(){
    if(this.data.deviceld==''){
     wx.showToast({
        title: '请先填写唯一标识',
        icon:'none'
      })
      return 
    }
    if(this.data.devicekey==''){
        wx.showToast({
          title: '请先填写设备秘钥',
          icon:'none'
        })
        return 
      }
    wx.request({
      url: 'https://zujieba.com/index/tt/tt',
      method:"POST",
      data:{type:1,status:1,deviceld:this.data.deviceld,devicekey:this.data.devicekey},
      success:(res)=>{
        console.log(res);
        
          if(res.data.success==1){
            wx.request({
              url: 'https://zujieba.com/index/tt/tt',
              method:"POST",
              data:{type:1,status:2,deviceld:this.data.deviceld,devicekey:this.data.devicekey},
              success:(res1)=>{
                     console.log(res1);
                     let shebeitype=[]
                     
                     res1.data.data.data.row.forEach((item)=>{
                       shebeitype.push(item.drivce_name)
                     })
                     this.setData({
                       shebeitype:shebeitype,
                       pintermodel:shebeitype[this.data.index3]
                       
                     })
              }
            })
          }else{
            wx.showToast({
              title:'未搜索到设备',
              icon:"none"
            })
          }
      },
      fail:(res)=>{
        console.log(res);
        
      }
    })
  },
  bindPickerChange3(e){
   console.log(e.detail.value);
   this.setData({
     index3:e.detail.value,
     pintermodel:this.data.shebeitype[e.detail.value]
   })
   
  }
})